/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.excesys.exsecs.om.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.log.annotation.SysLog;
import com.excesys.exsecs.om.entity.OmEvenAndFacility;
import com.excesys.exsecs.om.entity.OmEvenAndRegion;
import com.excesys.exsecs.om.entity.OmInspectionArea;
import com.excesys.exsecs.om.entity.OmInspectionItems;
import com.excesys.exsecs.om.mapper.OmEvenAndFacilityMapper;
import com.excesys.exsecs.om.mapper.OmEvenAndRegionMapper;
import com.excesys.exsecs.om.mapper.OmInspectionItemsMapper;
import com.excesys.exsecs.om.service.OmInspectionAreaClassificationListService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 巡查区域列表
 *
 * @author
 * @date 2023-05-19
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/ominspectionList")
@Tag(name = "巡查区域列表")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class OmInspectionListController {

	private final OmInspectionAreaClassificationListService omInspectionAreaClassificationListService;

	private final OmEvenAndFacilityMapper omEvenAndFacilityMapper;

	private final OmEvenAndRegionMapper omEvenAndRegionMapper;

	private final OmInspectionItemsMapper omInspectionItemsMapper;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param omInspectionArea 巡检区域列表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('om_ominspectionList_get')")
	public R getOmInspectionPage(Page page, OmInspectionArea omInspectionArea) {
		List<OmInspectionArea> list = omInspectionAreaClassificationListService
				.getAreaList(omInspectionArea.getInspectionCode(), omInspectionArea.getInspectionName());
		Page pages = omInspectionAreaClassificationListService.setPage(list, page);
		return R.ok(pages);
	}

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param omInspectionArea 巡检区域列表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/query")
	@PreAuthorize("@pms.hasPermission('om_ominspectionList_get')")
	public R getOmInspectionQuery(Page page, OmInspectionArea omInspectionArea) {
		List<OmInspectionArea> list = omInspectionAreaClassificationListService
				.getAreaList(omInspectionArea.getInspectionCode(), omInspectionArea.getInspectionName());
		return R.ok(list);
	}

	/**
	 * 递增编号
	 * @return
	 */
	@GetMapping("/getCode/{exId}")
	@PreAuthorize("@pms.hasPermission('om_ominspectionList_get')")
	public R getCode(@PathVariable("exId") Long exId) {
		return R.ok(omInspectionAreaClassificationListService.getList(exId + ""));
	}

	/**
	 * 通过id查询巡检区域列表
	 * @param exId id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{exId}")
	@PreAuthorize("@pms.hasPermission('om_ominspectionList_get')")
	public R getById(@PathVariable("exId") Integer exId) {
		return R.ok(omInspectionAreaClassificationListService.getById(exId));
	}

	/**
	 * 通过id查询巡检区域列表
	 * @param numId
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/getByNumId/{numId}")
	@PreAuthorize("@pms.hasPermission('om_ominspectionList_get')")
	public R getByNumId(@PathVariable("numId") String numId) {
		return R.ok(omInspectionAreaClassificationListService.getLists(numId));
	}

	/**
	 * 新增巡检区域列表
	 * @param omInspectionArea 巡检区域列表
	 * @return R
	 */
	@Operation(summary = "新增巡检区域列表", description = "新增巡检区域列表")
	@SysLog("新增巡检区域列表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('om_ominspectionList_add')")
	public R save(@RequestBody OmInspectionArea omInspectionArea) {
		List<OmEvenAndFacility> list = omEvenAndFacilityMapper
				.getchlidList(omInspectionArea.getInspectionClassification());
		if (list.size() > 0) {
			List<OmEvenAndRegion> regionList = omEvenAndRegionMapper
					.getRegionList(omInspectionArea.getInspectionClassification());
			for (OmEvenAndRegion omEvenAndRegion : regionList) {
				OmInspectionItems omInspectionItems = omInspectionItemsMapper
						.getItemsById(omEvenAndRegion.getItemsId());
				OmInspectionItems om = new OmInspectionItems();
				om.setEvenInfo(omInspectionItems.getEvenInfo());
				om.setEvenCriterion(omInspectionItems.getEvenCriterion());
				om.setEvenSwitch(omInspectionItems.getEvenSwitch());
				omInspectionItemsMapper.insert(om);
				OmEvenAndFacility omEvenAndFacility = new OmEvenAndFacility();
				omEvenAndFacility.setRegionFacilityCode(omInspectionArea.getInspectionCode());
				omEvenAndFacility.setRegionFacilityName(omInspectionArea.getInspectionName());
				omEvenAndFacility.setParentId(omInspectionArea.getInspectionClassification());
				omEvenAndFacility.setItemsId(om.getId().toString());
				omEvenAndFacilityMapper.insert(omEvenAndFacility);
			}
		}
		return R.ok(omInspectionAreaClassificationListService.save(omInspectionArea));
	}

	/**
	 * 修改巡检区域列表
	 * @param omInspectionArea 巡检区域列表
	 * @return R
	 */
	@Operation(summary = "修改巡检区域列表", description = "修改巡检区域列表")
	@SysLog("修改巡检区域列表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('om_ominspectionList_edit')")
	public R updateById(@RequestBody OmInspectionArea omInspectionArea) {
		return R.ok(omInspectionAreaClassificationListService.updateById(omInspectionArea));
	}

	/**
	 * 通过id删除巡检异常表
	 * @param exId id
	 * @return R
	 */
	@Operation(summary = "通过id删除巡检区域列表", description = "通过id删除巡检区域列表")
	@SysLog("通过id删除巡检区域列表")
	@DeleteMapping("/{exId}")
	@PreAuthorize("@pms.hasPermission('om_ominspectionList_del')")
	public R removeById(@PathVariable Integer exId) {
		return R.ok(omInspectionAreaClassificationListService.removeById(exId));
	}

	@Operation(summary = "通过ids批量删", description = "通过ids批量删")
	@SysLog("通过ids删除巡检列表-巡检列表")
	@DeleteMapping("/deleteIds")
	@PreAuthorize("@pms.hasPermission('om_ominspectionList_del')")
	public R removeByIds(@RequestBody List<String> ids) {
		return R.ok(omInspectionAreaClassificationListService.removeByIds(ids));
	}

}
